# -*- coding: utf-8 -*-

from ftplib import FTP
import time
import os
import pexpect

serverip = 'xxxx'
port = 22
username = ['username1','...']
password = ['password1','...']

filename = 'test.txt'
dir_upload_name='upload'

buf_size = 1024

def getftpfile(ftp,localfilename,remotefilename):
    f_local_handler = open(localfilename,'wb')
    ftp.retrbinary('RETR '+remotefilename,f_local_handler.write,buf_size)
    f_local_handler.close()
    
    
def getTodayStr():
    today = str(time.strftime('%Y%m%d',time.localtime(time.time())))
    print('today is:'+today+'\n')
    return today
    
def getftpFileList(ftp,username):
    print (ftp.getwelcome())
    ftpfilelist = ftp.nlst()
    today = getTodayStr()
    #today upload filelist
    todayfilelist = []
    for file in ftpfilelist:
        if today in file and file.endswith('.txt')and file.startswith(username):
            todayfilelist.append(file)
    
    return todayfilelist
    
def getlocalxxxx(filename):
    xxxx = []
    print (filename)
    with open(filename,"r") as file:
        for line in file.readlines():
            temp_xxxx = line.split(',')[2]
            #need judge length?
            if len(temp_xxxx)==11:
                xxxx.append(temp_xxxx)
            
    return xxxx

def setfile():
    password = '''xxxx'''
    command = '''scp xxxx.txt root@xxxx:/home/filter/hashTable'''
    scp = pexpect.spawn(command,timeout=10)
    scp.expect('password:')
    scp.sendline(password)
    scp.read()
    
    

def main():
    
    localfilelist = []
    sumfile = open('xxxx.txt','a')
    fenshengfile = open('xxxx.txt','w')
    
    for i in range(0,len(username)):
        ftp=FTP(serverip)
        ftp.set_debuglevel(2)
        ftp.login(username[i],password[i])
        ftp.cwd(dir_upload_name)
        #get today or yestoday?
        todayfilelist = getftpFileList(ftp,username[i])
        print (todayfilelist)
        if len(todayfilelist)==0:
            continue
        #get gesheng today file list    
        for file in todayfilelist:
            getftpfile(ftp,file,file)
            localfilelist.append(file)
            
        temp_list = getlocalxxxx(file)
        for j in temp_list:
            fenshengfile.write(str(j)+','+username[i]+'\n')
                     
        ftp.quit()
        
    for localfile in localfilelist:
        allxxxx_list = getlocalxxxx(localfile)
        for i in allxxxx_list:
            sumfile.write(str(i)+'\n')
            
    fenshengfile.close()
    sumfile.close()
    
    
    setfile()
    
if __name__=='__main__':
    main()


